package query

import (
	"strings"

	"ssalarm/common/dto"
)

type SysAlarmQuery struct {
	dto.Pagination `search:"-"`
	dto.OrderBy    `search:"-"`
	dto.UserInfo

	SnIn          []string `form:"snIn" search:"type:in;column:dev_sn;table:sys_alarm"`
	SnLike        string   `form:"sn" search:"type:contains;column:dev_sn;table:sys_alarm"`
	AlarmType     string   `form:"alarmType" search:"type:contains;column:alarm_type;table:sys_alarm"`
	VarKey        string   `form:"varKey" search:"type:contains;column:var_key;table:sys_alarm"`
	VarName       string   `form:"varName" search:"type:contains;column:var_name;table:sys_alarm"`
	DeptIdIn      []string `form:"depIdIn" search:"type:in;column:dept_id;table:sys_alarm"`
	Search        string   `form:"search" search:"type:likeOr;column:dev_sn,var_key,var_name,alarm_type;table:sys_alarm"`
	StartTime     string   `form:"startTime" search:"type:gte;column:alarm_time;table:sys_alarm"`
	EndTime       string   `form:"endTime" search:"type:lte;column:alarm_time;table:sys_alarm"`
	ProcessStatus *bool    `form:"processStatus" search:"-"`
	DeptId        string   `form:"deptId" search:"-"`
	AlarmLevel    int      `form:"alarmLevel" search:"type:exact;column:level;table:sys_alarm"`
	AlarmMode     int      `form:"alarmMode" search:"type:exact;column:alarm_mode;table:sys_alarm"`
	DevType       string   `form:"devType" search:"type:exact;column:dev_type;table:sys_alarm"`
	DevTypeIn     []string `form:"devTypeIn" search:"type:in;column:dev_type;table:sys_alarm"`

	ProcessNotDone bool     `search:"type:ne;column:process_status;table:sys_alarm"`
	ProcessDone    bool     `search:"type:exact;column:process_status;table:sys_alarm"`
	IdIn           []string `search:"type:in;column:id;table:sys_alarm"`
}

func (m *SysAlarmQuery) GetNeedSearch() interface{} {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DeptIdIn = m.DeptIdList
		}
	} else {
		m.DeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	if m.ProcessStatus != nil {
		if *m.ProcessStatus {
			m.ProcessDone = true
		} else {
			m.ProcessNotDone = true
		}
	}
	return *m
}
